﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using System.Web;
using YouHP.Model.Group;

namespace YouHP.Pages
{
    public class TreatmentList : BasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if(ErrorCount > 0) return;
            
            HandleCommon();
            if(ErrorCount > 0) return;
            
            if(IsPost) HandlePost();
            else HandleGet();
        }

        protected List<TopicInfo> TopicDataListHottest;
        protected List<TopicInfo> TopicDataListLatest;
        protected List<UserInfo> UserDataListLatest;

        protected List<TreatmentInfo> TreatmentDataList;
        protected List<DiseaseInfo> DiseaseLst;
        protected int TreatmentCount = 0;
        protected int TreatmentPageCount = 0 ;
        protected int TreatmentPageSize = 15;
        protected int TreatmentPageIndex = 1;
        protected string TreatmentPagingString;

        protected string ItemUrl = "/Treatment.aspx?id=";
        protected string ItemOperName = "查看";

        protected TreatmentSearchCondition cond = null;

        protected void HandleCommon()
        {
            this.AddLocation("治疗列表", "/TreatmentAll.aspx");
            DiseaseLst = DiseaseBLL.Instance.GetAllDiseaseByName("");
            this.SetTitle("治疗列表");
        }
        
        protected void HandlePost()
        {
            
        }

        protected void HandleGet()
        {
            int topiccnt = 8;
            TopicDataListHottest = TopicBLL.Instance.GetHottestTopic(topiccnt);
            TopicDataListLatest = TopicBLL.Instance.GetLastestTopic(topiccnt);
            UserDataListLatest = PatientBLL.Instance.GetLastestPatient(topiccnt);

            TreatmentPageIndex = RequestTools.GetQueryInt("page", 1);

            cond = new TreatmentSearchCondition();
            cond.DiseaseId = RequestTools.GetQueryInt("dszid", 0);
            cond.DiseaseName = RequestTools.GetQueryString("dszname").Trim();
            cond.TreatmentName = RequestTools.GetQueryString("trmname").Trim();
            cond.FirstLetter = RequestTools.GetQueryString("firstLetter").Trim();

            TreatmentDataList = TreatmentBLL.Instance.GetTreatmentList(
                TreatmentPageSize, TreatmentPageIndex,
                cond, out TreatmentCount);

            TreatmentPageCount = Utils.GetPageCount(TreatmentCount, TreatmentPageSize);

            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            TreatmentPagingString = this.GetPagingString(
                TreatmentPageIndex,
                TreatmentPageCount,
                "/TreatmentList.aspx" + query, "page");

            string sel = RequestTools.GetQueryString("sel");
            if (sel == "th")
            {
                this.ItemUrl = "/TreatmentHistory/HistoryStart.aspx?trmid=";
                this.ItemOperName = "选择";
            }

        }

        protected string GetCondStr(TreatmentSearchCondition cond)
        {
            string condStr = String.Empty;
            bool firstCond = true;

            if (cond == null)
            {
                return string.Empty;
            }

            //不考虑其它条件
            if (cond.FirstLetter.Length > 0)
            {
                condStr = string.Format(" 拼音首字母({0}) ", cond.FirstLetter);
                return condStr;
            }

            if (cond.DiseaseId > 0)
            {
                condStr += DiseaseBLL.Instance.GetDiseaseInfo(cond.DiseaseId).Name;
                firstCond = false;
            }

            if (cond.TreatmentName.Length > 0)
            {
                if (!firstCond)
                {
                    condStr += " + ";
                }
                condStr += cond.TreatmentName;
                firstCond = false;
            }

            return condStr;
        }
    }
}